#ifndef _LOCO_H_INCLUDE_
#define _LOCO_H_INCLUDE_

#include "common.h"
#include "wxpolygonex.h"
#include "wxline.h"
#include "nodemanager/wnodemanager.h"

class Loco : public NedAllocatedObject
{
public:
    Loco();
    Loco(wxString locoName);
    ~Loco();

    /// 得到机车的包围盒
    inline wxPolygonEx* getBoundBox(void) { return &mBoundingBox; }
    /// 设置机车名称
    inline void setName(wxString name) { mLocoName = name; }
    /// 得到机车名称
    inline wxString getName(void) { return mLocoName; }
    /// 设置机车占用区段
    inline void setSectNode(tagSectNode *sectnode) { mSectNode = sectnode; }
    /// 得到机车占用区段
    inline tagSectNode* getSectNode(void) { return mSectNode; }
    /// 得到左边机车
    inline Loco* getLeftLoco(void) { return mLeftLoco; }
    /// 设置左边机车
    inline void setLeftLoco(Loco *ploco) { mLeftLoco = ploco; }
    /// 得到右边机车
    inline Loco* getRightLoco(void) { return mRightLoco; }
    /// 设置右边机车
    inline void setRightLoco(Loco *ploco) { mRightLoco = ploco; }
    /// 设置名称显示坐标
    inline void setNamePosition(wxPoint pos) { mLocoNamePos = pos; }
    /// 得到名称显示坐标
    inline wxPoint getNamePosition(void) { return mLocoNamePos; }
    /// 检测当前轨道是否被选中
    inline bool isSelected(void) { return mSelected; }

    /// 更新
    void update(void);
    /// 绘制
    void draw(maindrawscene *painter);
    /// 处理鼠标事件
    bool OnMMouseEvent(wxMouseEvent event);

public:
    bool m_mouseOvered;
    wxPoint m_mousePosition;

private:
    wxPolygonEx mBoundingBox;
    wxString mLocoName;
    wxPoint mLocoNamePos;
    tagSectNode *mSectNode;
    Loco *mLeftLoco,*mRightLoco;
    bool mSelected;
};

class Track : public NedAllocatedObject
{
public:
    Track();
    ~Track();

    /// 设置机车占用区段
    void setSectNode(tagSectNode *sectnode);
    /// 得到机车占用区段
    inline tagSectNode* getSectNode(void) { return mSectNode; }

    /// 添加指定机车到轨道上
    bool addLoco(Loco* ploco,SwitchState state,int dir=0);
    /// 添加机车到指定机车的前面或者后面
    bool addLocos(std::vector<Loco*> srcLoco,Loco* decLoco);
    /// 删除指定机车
    bool delLoco(Loco* ploco);
    /// 通过轨道方向删除机车
    std::vector<Loco*> delLocoByTrack(int state=SwitchState::POSITIVE,int dir=0);
    /// 连接机车
    bool couplingLoco(Loco* leftLoco,Loco *rightLoco);
    /// 分离机车
    bool uncouplingLoco(Loco* leftLoco,Loco *rightLoco);
    /// 检测指定机车是否存在
    bool isExistLoco(Loco* ploco);
    /// 检测当前轨道是否被选中
    SwitchState isSelected(void);
    /// 删除所有机车
    void clearAllLocos(void);

    /// 更新
    void update(void);
    /// 绘制
    void draw(maindrawscene *painter);
    /// 处理鼠标事件
    bool OnMMouseEvent(wxMouseEvent event);

private:
    tagSectNode *mSectNode;
    std::map<SwitchState,wxLine> mTracks;
    std::map<SwitchState,bool> mTrackSelecteds;
    std::map<SwitchState,wxPolygonEx> mBoundingBox;
    std::map<SwitchState,std::deque<Loco*>> mTrackLocos;
};

#endif // LOCO_H
